package com.ghc.util;

import java.util.ArrayList;

public class PageUtil<T> {
	
	private int pageSize;//每页行数
	private int pageCount;//总页数
	private int pageCurrent;//当前页数
	private int allrow;//总行数
	private ArrayList<Object> list = new ArrayList<Object>(0);//用来接收查询结果
	
	/**
	* @Title: init 
	* @Description: 初始化分页 
	* @param  
	* @return void 
	* @throws
	 */
	public void init() {
		this.setFirstPage(isFirstPage());
		this.setLastPage(isLastPage());
		this.setHasPreviousPage(isHasPreviousPage());
		this.setHasNextPage(isHasNextPage());
	}
	
	public boolean isFirstPage() {
		return pageCurrent == 1;// 如果当前页是第一页
	}

	public boolean isLastPage() {
		return pageCurrent == pageCount;// 如果当前页是最后一页
	}

	public boolean isHasPreviousPage() {
		return pageCurrent != 1;// 只要当前页不是第1页
	}

	public boolean isHasNextPage() {
		return pageCurrent != pageCount;// 只要当前页不是最后一页
	}
	
	/**
	 * * 计算总页数静态方法,供外部直接通过类名调用
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param allrow
	 *            总记录数 
	 * @return 总页数
	 */
	public static int countPage(final int pageSize, final int allrow) {
		int pageCount = allrow % pageSize == 0 ? allrow / pageSize : allrow / pageSize + 1;
		return pageCount;
	}
	
	/**
	 * * 计算当前页开始记数
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param pageCurrent
	 *            当前第几页
	 * @return 当前页开始记录号
	 */
	public static int start(final int pageSize, final int pageCurrent) {
		final int start = pageSize * (pageCurrent - 1);
		return start;
	}
	
	/**
	 * * 计算当前页若为0或空请求的URL中没有page=",则用1代替 *
	 * 
	 * @param page
	 *            传入的参数可能为空,返1,则返page *
	 * @return 当前页
	 */
	public static int CurrentPage(int page) {
		final int pageCurrent = (page == 0 ? 1 : page);
		return pageCurrent;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageCurrent() {
		return pageCurrent;
	}
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}
	public ArrayList<Object> getList() {
		return list;
	}
	public void setList(ArrayList<Object> list) {
		this.list = list;
	}
	public int getAllrow() {
		return allrow;
	}
	public void setAllrow(int allrow) {
		this.allrow = allrow;
	}

	public void setFirstPage(boolean isFirstPage) {
	}

	public void setLastPage(boolean isLastPage) {
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
	}

	public void setHasNextPage(boolean hasNextPage) {
	}
}
